package com.easyattendance.services;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.joda.time.DateTimeConstants;
import org.joda.time.Days;
import org.joda.time.LocalDate;

import com.easyattendance.model.Holidays;
import com.easyattendance.util.HibernateUtil;

public class AddHolidayListService {

	/* Method to CREATE a holiday in the database */
	   public synchronized boolean addHolidays(Holidays holidays){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();
        	 session.save(holidays);
           	 tx.commit();
       
           	 result=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return result;
	   }
	   
	   /* check whether holiday already exists in the database */
	   public boolean ifAlreadyExists(Holidays newHolidays) {
			 boolean flag = false;
			 
			 Session session = HibernateUtil.openSession();
		      Transaction tx = null;
		      List sections = null;
		      try{
		         tx = session.beginTransaction();
		         sections = session.createQuery("FROM Holidays").list(); 
		         for (Iterator iterator = sections.iterator(); iterator.hasNext();){
		        	 Holidays holiday = (Holidays) iterator.next(); 
		            if(holiday.equals(newHolidays)) {
		            	System.out.println("Holidays already exists");
		            	flag = true;
		            	break;
		            }
		         }
		         tx.commit();
		      }catch (HibernateException e) {
		         if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
		      }finally {
		         session.close(); 
		      }
			 
			 return flag;
		}
	   
	   /* get total no of holidays in a section between two days */
	   public int getTotalHolidaysBetweenDates(String sectionId, java.sql.Timestamp start, java.sql.Timestamp end) {
			 boolean flag = false;
			 int total = 0;
			 Session session = HibernateUtil.openSession();
		      Transaction tx = null;
		      List sections = null;
		      try{
		         tx = session.beginTransaction();
		         sections = session.createQuery("FROM Holidays where holiday_start_date>='"+start+"' and holiday_start_date<='"+end
		        		 +"' and holiday_end_date>='"+start+"' and holiday_end_date<='"+end+"' and section_id='"+sectionId+"'").list(); 

		         total = sections.size();
		         tx.commit();
		      }catch (HibernateException e) {
		         if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
		      }finally {
		         session.close(); 
		      }
			 
			 return total;
		}
	   
	   /* Get total no of days between 2 dates*/
	   public int getTotalDays(LocalDate start, LocalDate end) {
		   return Days.daysBetween(end, start).getDays();
	   }
	   
	   /* Get all working days excluding saturdays and sundays*/
	   public int getWorkingDaysExcludingWeekends(LocalDate start, LocalDate end) {
		   
		    LocalDate weekday = start;
		    int total = 0;

		    if (start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
		    	
		        weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
		    }

		    while (weekday.isBefore(end)) {
		    	//weekends++;
		        System.out.println(weekday);

		        if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
		            weekday = weekday.plusDays(3);
		        else {
		        	total++;
		            weekday = weekday.plusDays(1);
		        }
		    }
		    return total;
		}
}
